// Copyright 2011 Jay Young. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

/**
 * @fileoverview
 *
 * Handler class for browsing through all posts.
 */

goog.provide('rb.handlers.BrowseHandler');

goog.require('rb.browse.Browser');

goog.require('relief.nav.Handler');



/**
 * Handler class for browsing through all posts.  Sets up the browsing
 * UI.
 *
 * @param {rb.ServiceProvider} sp The app's service provider.
 *
 * @constructor
 * @implements {relief.nav.Handler}
 */
rb.handlers.BrowseHandler = function(sp) {
  /**
   * @type {rb.ServiceProvider}
   * @private
   */
  this.sp_ = sp;

  /**
   * @type {rb.browse.Browser}
   * @private
   */
  this.browser_ = null;
};


/**
 * @inheritDoc
 */
rb.handlers.BrowseHandler.prototype.handle = function(path) {
  var browser = this.browser_;
  if (! browser) {
    browser = this.browser_ = new rb.browse.Browser(this.sp_);
  }

  browser.setUp();
};


/**
 * @inheritDoc
 */
rb.handlers.BrowseHandler.prototype.transition = function(path, onTransition) {
  this.handle(path);
  onTransition(true);
};


/**
 * @inheritDoc
 */
rb.handlers.BrowseHandler.prototype.exit = function(onExit, force) {
  onExit(true);
};


/**
 * @inheritDoc
 */
rb.handlers.BrowseHandler.prototype.dispose = function() {
  goog.dispose(this.browser_);
  this.browser_ = this.sp_ = null;
};


/**
 * @inheritDoc
 */
rb.handlers.BrowseHandler.prototype.isDisposed = function() {
  return this.sp_ === null;
};
